﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace AlphaSolutionsGP
{

    public partial class frmGradeView : Form
    {
        private Grade aGrade;
        private string studentId;
        private bool toModify;

        //constructor for new grade
        public frmGradeView(string studentID)
        {
            toModify = false;
            studentId = studentID;
            aGrade = new Grade();
            InitializeComponent();
            dteDateSub.MaxDate = DateTime.Today;

            //default values
            fillInCombo(cmdAssessmentSelection, "assessment_ID", "ASSESSMENT");
            txtStudentIDGrade.Text = studentID;
            dteDateSub.Format = DateTimePickerFormat.Custom;
            //dteDateSub.CustomFormat = " "; //set the date picker display to blank value


        }


        ////constructor with modifiable grade
        public frmGradeView(Grade g)
        {
           
            toModify = true;
            aGrade = g;
            InitializeComponent();
            printData();
            dteDateSub.MaxDate = DateTime.Today;
        }


        //method to print details of the qualification when in modify mode
        private void printData()
        { 
          txtStudentIDGrade.Text = aGrade.SudentID;
          fillInCombo(cmdAssessmentSelection, "assessment_ID", "ASSESSMENT");
          cmdAssessmentSelection.SelectedItem = aGrade.AssID.ToString();
          dteDateSub.Value = convertFromMySQLDate(aGrade.DateSubmission);
          cmdGrade.Text =  aGrade.AssGrade;
        }


        private void bntHelp_Click(object sender, EventArgs e)
        {
            MessageBox.Show("You must fill in all the fields. \nIf you came to this window by modifying a qualification, remember to check all the details are correct before submitting. ", "WUC Help", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        //qualification fields validation
        private bool gradeFieldsValidation()
        {
            bool flag = true;
            string msg = "";
            if (cmdAssessmentSelection.Text == "")
            {
                msg += "\n- You must select an Assessment ID";
                flag = false;
            }
            if (cmdGrade.Text == "")
            {
                msg += "\n- You must choose a Grade";
                flag = false;
            }

            if (!flag)
                MessageBox.Show(msg, "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return flag;
        }


        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private void frmGrade_Load(object sender, EventArgs e)
        {

        }
       

        private void txtStudentID_TextChanged(object sender, EventArgs e)
        {
            
        }


        private void fillInCombo(ComboBox cb, string column, string table)
        {
            MySqlConnection connection = new MySqlConnection("server=194.81.104.22;" + "User Id=alpha;" + "database=alpha_db;" + "password=bravo;");
            try
            {
                connection.Open();
                MySqlCommand cmd = connection.CreateCommand();
                cmd.CommandText = "SELECT DISTINCT " + column + " FROM " + table + ""; //select the biggest staff id
                MySqlDataReader myReader = cmd.ExecuteReader();
                string nameColumn = "" + column + "" ;
                while (myReader.Read())
                {
                    column = myReader.GetString(nameColumn);
                    cb.Items.Add(column);
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("Unable to fill table due to the following error:\n\n" + ex.ToString());
            }

            finally
            {
                connection.Close();

            }
        }


        private void btnSave_Click(object sender, EventArgs e)
        {
            if (gradeFieldsValidation())
            {
                if (toModify)
                {
                    //update aGrade with new values
                    aGrade.SudentID = txtStudentIDGrade.Text;
                    aGrade.AssID = (int)Int32.Parse(cmdAssessmentSelection.Text);
                    aGrade.DateSubmission = dteDateSub.Text;
                    aGrade.AssGrade = cmdGrade.Text;
                    
               
                    //this is where a grade has been modified so needs to be put back into the arraylist.
                    aGrade.amendGrade(aGrade.ResultID.ToString());
                    this.Close();

                }
                //else new grade and add
                else
                {

                    Grade theGrade = new Grade(aGrade.ResultID, txtStudentIDGrade.Text, Int32.Parse(cmdAssessmentSelection.Text), dteDateSub.Text.ToString(), cmdGrade.Text);
                    try
                    {
                        theGrade.createGrade(studentId);
                        this.Close();
                    }
                    catch
                    {
                        MessageBox.Show("Failed to create the grade.");
                    }

                }
            }
        }


        public static DateTime convertFromMySQLDate(string dt)
        {
            //string dateToString = Convert.ToString(date);
            DateTime firstFormatDate = DateTime.Parse(dt);
            string dateString = firstFormatDate.ToString("dd/MM/yyyy HH:mm:ss");
            DateTime finalFormatDate = DateTime.Parse(dateString);
            return finalFormatDate;
        }


        private void dteDateSub_ValueChanged(object sender, EventArgs e)
        {
            dteDateSub.Value = dteDateSub.Value;
        }
    }
}
